/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 */

package src.clovece;

import java.awt.*;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import src.clovece.game.GameInfo;
import src.clovece.game.GameManagement;


/**
 *  Class serves graphic container for the whole game board and info panel
 */
public class GamePanel extends JPanel implements MouseListener {
  private JLayeredPane gamePane = null;
  private GameInfoPanel infoPanel = null;
  private GameManagement gm = GameManagement.getGameManagementInstance();
  private GameInfo gameInfo = GameInfo.getGameInfoInstance();

    public GamePanel (GameFrame parent) {
        super(null);

        this.setLayout(null);
        this.gamePane = new JLayeredPane();
        this.gamePane.setSize(new Dimension(768,768));
        this.gamePane.setLocation(0,0);

        this.gm.setGameFrame(parent);

        /*
         *  Create game board for single player game
         */
        if(this.gameInfo.getLocalGame()) {
            if(this.gameInfo.getNewGame()) {
                this.gm.createNewSingleplayerGameBoard();
            }
            else if(this.gameInfo.getLoadedGame()) {
                this.gm.createLoadedSingleplayerGameBoard();
            }
        }
        /*
         *  Create game board for multi player game
         */
        else {
            if(this.gameInfo.getNewGame()) {
                this.gm.createNewMultiplayerGameBoard();
            }
            else if(this.gameInfo.getLoadedGame()) {
                this.gm.createLoadedMultiplayerGameBoard();
            }
            else if(this.gameInfo.getJoinedGame()) {
                this.gm.createJoinedMultiplayerGameBoard();
            }
        }
        for(int i = 0; i < this.gameInfo.getPlayers().length; i++) {
            System.out.println(this.gameInfo.getPlayers()[i].getNickname());
            for(int j = 0; j < 4; j++) {
                this.gamePane.add(this.gameInfo.getPlayers()[i].getFigurines()[j]);
                this.gamePane.setLayer(this.gameInfo.getFields()[i],JLayeredPane.DRAG_LAYER);
            }
            this.gamePane.add(this.gameInfo.getPlayers()[i]);
            this.gamePane.setLayer(this.gameInfo.getPlayers()[i],JLayeredPane.DEFAULT_LAYER);
        }
        for(int i = 0; i < this.gameInfo.getFields().length; i++) {
            this.gamePane.add(this.gameInfo.getFields()[i], new Integer(i));
            this.gamePane.setLayer(this.gameInfo.getFields()[i],JLayeredPane.DEFAULT_LAYER);
        }
        for(int i = 0; i < this.gameInfo.getNumberOfPlayers(); i++) {
            int x,y;
            JLabel arrow = new JLabel();
            if(this.gameInfo.getGameBoardSize() == 36) {
                if(i == 0) {
                    x = this.gameInfo.getFields()[27].getX()-40;
                    y = this.gameInfo.getFields()[27].getY();
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_right.png"));
                }
                else if(i == 1) {
                    x = this.gameInfo.getFields()[9].getX()+40;
                    y = this.gameInfo.getFields()[9].getY();
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_left.png"));
                }
                else if(i == 2) {
                    x = this.gameInfo.getFields()[0].getX();
                    y = this.gameInfo.getFields()[0].getY()-40;
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_down.png"));
                }
                else {
                    x = this.gameInfo.getFields()[18].getX();
                    y = this.gameInfo.getFields()[18].getY()+40;
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_up.png"));
                }
            }
            else if(this.gameInfo.getGameBoardSize() == 48) {
                if(i == 0) {
                    x = this.gameInfo.getFields()[37].getX()-40;
                    y = this.gameInfo.getFields()[37].getY();
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_right.png"));
                }
                else if(i == 1) {
                    x = this.gameInfo.getFields()[13].getX()+40;
                    y = this.gameInfo.getFields()[13].getY();
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_left.png"));
                }
                else if(i == 2) {
                    x = this.gameInfo.getFields()[1].getX();
                    y = this.gameInfo.getFields()[1].getY()-40;
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_down.png"));
                }
                else {
                    x = this.gameInfo.getFields()[25].getX();
                    y = this.gameInfo.getFields()[25].getY()+40;
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_up.png"));
                }
            }
            else {
                if(i == 0) {
                    x = this.gameInfo.getFields()[46].getX()-40;
                    y = this.gameInfo.getFields()[46].getY();
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_right.png"));
                }
                else if(i == 1) {
                    x = this.gameInfo.getFields()[16].getX()+40;
                    y = this.gameInfo.getFields()[16].getY();
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_left.png"));
                }
                else if(i == 2) {
                    x = this.gameInfo.getFields()[1].getX();
                    y = this.gameInfo.getFields()[1].getY()-40;
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_down.png"));
                }
                else {
                    x = this.gameInfo.getFields()[31].getX();
                    y = this.gameInfo.getFields()[31].getY()+40;
                    arrow.setBounds(x, y, 40, 40);
                    arrow.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                                    + "lib" + System.getProperty("file.separator") + "img"
                                    + System.getProperty("file.separator") + "arrow_up.png"));
                }
            }
            this.gamePane.add(arrow);
        }
        
        this.add(gamePane);
        this.infoPanel = new GameInfoPanel();
        this.gm.setInfoPanel(this.infoPanel);
        this.infoPanel.setSize(256, 768);
        this.infoPanel.setLocation(768, 0);
        this.infoPanel.showYourself();
        this.infoPanel.repaint();
        this.add(this.infoPanel);
        this.setBackground(new Color(255,225,70));
        this.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
        this.gm.deselectFigurine();
    }

    public void mousePressed(MouseEvent e) {
        mouseClicked(e);
    }

    public void mouseReleased(MouseEvent e) {
        mouseClicked(e);
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}
